import pandas as pd
import numpy as np

# 读取 Excel 表格数据
data = pd.read_excel(r'excel.file/荷花年份酒数据_向量.xlsx',header=None)

# 获取样本名称和属性向量
samples = data.iloc[0, :].values
vectors = data.iloc[1:, :].values

print(samples)
print(vectors)
# 计算相似度矩阵
def calculate_similarity(vector1, vector2):
    numerator = np.sum(np.abs(vector1 - vector2) / (vector1 + vector2))
    #print((vector1 - vector2))
    similarity = 1 - (1 / len(vector1)) * numerator
    return similarity

similarity_matrix = np.zeros((len(samples), len(samples)))
for i in range(len(samples)):
    for j in range(i, len(samples)):
        similarity_matrix[i, j] = calculate_similarity(vectors[i], vectors[j])
        similarity_matrix[j, i] = similarity_matrix[i, j]  # 相似度矩阵是对称的

# 将相似度矩阵写入 Excel 表格
output_data = pd.DataFrame(similarity_matrix, index=samples, columns=samples)
output_data.to_excel(r'excel.file/similar.xlsx')